from multiprocessing import Queue
import threading
from Pump.bean.pump import Pump
from Pump.bean.pipe import Pipe
from Pump.bean.pool import FluidPool
from Pump.bean.junction import OneTotwo
import time


class engine():
    if __name__ == '__main__':
        # 一号泵所用队列
        Q1_num1 = Queue()  # 用于 计算扬程
        Q2_num1 = Queue()  # 用于左端连接pipe
        Q3_num1 = Queue()  # 用于右端连接pipe
        H_num1 = Queue()  # 扬程
        # 二号泵所用队列
        Q1_num2 = Queue()
        Q2_num2 = Queue()
        Q3_num2 = Queue()
        H_num2 = Queue()

        # junction1所用队列
        jt1_flow_in = Queue()
        jt1_flow_out1 = Queue()  # Q2_num1
        jt1_flow_out2 = Queue()  # Q2_num2

        # junction2所用队列
        jt2_flow_in = Queue()
        jt2_flow_out1 = Queue()  # Q3_num1
        jt2_flow_out2 = Queue()  # Q3_num2

        # 用于存储频率
        frequency1 = Queue()
        frequency2 = Queue()

        # 用于pipe和pool连接
        pipe1_pool1 = Queue()
        pipe2_pool2 = Queue()

        pump1 = Pump(1.23, 2.22, 23.55, 0, 0, 2, 0.00814, 380, 50000, 90)  # 一号泵
        pump2 = Pump(1.23, 2.22, 23.55, 0, 0, 2, 0.00814, 380, 50000, 90)  # 二号泵

        pipe1 = Pipe(1.22, 2.33, 5.8, 'pool1', 'junction1', 5.9, 0.2, 3.1, 0.0)  # 连接pool1和junction1
        pipe2 = Pipe(1.22, 2.33, 5.8, 'junction2', 'pool2', 5.9, 0.2, 3.1, 0.0)  # 连接pool2和junction2

        # pipe3 = Pipe()#连接junction1和pump1
        # pipe4 = Pipe()#连接junction1和pump2
        # pipe5 = Pipe()#连接junction2和pump1
        # pipe6 = Pipe()#连接junction2和pump2

        oneTotwo1 = OneTotwo(1.33, 2.54, 4.22, 0, 0, 0, 0)
        oneTotwo2 = OneTotwo(1.33, 2.54, 4.22, 0, 0, 0, 0)

        # 抽水水池
        pool_1 = FluidPool(5, 6, 50, 15, 1.452, 2.3454, 2.2, 45, 10, 5 * 6 * 15)
        # 蓄水水池
        pool_2 = FluidPool(5, 6, 50, 40, 1.452, 2.3454, 2.2, 45, 10, 5 * 6 * 40)

        produceFrequency1 = threading.Thread(target=pump1.produceFrequency, args=(frequency1,), name='frquency1')
        produceFrequency2 = threading.Thread(target=pump2.produceFrequency, args=(frequency2,), name='frquency1')

        produceFlow1 = threading.Thread(target=pump1.produceFlow, args=(frequency1, Q1_num1, Q2_num1, Q3_num1),
                                        name='procuceFlow')
        produceRaise1 = threading.Thread(target=pump1.produceRaise, args=(Q1_num1, H_num1), name='procuceRaise')

        produceFlow2 = threading.Thread(target=pump2.produceFlow, args=(frequency2, Q1_num2, Q2_num2, Q3_num2),
                                        name='procuceFlow')
        produceRaise2 = threading.Thread(target=pump2.produceRaise, args=(Q1_num2, H_num2), name='procuceRaise')

        junction_1 = threading.Thread(target=oneTotwo1.computeFlow, args=(Q2_num1, Q2_num2, jt1_flow_in),
                                      name='junction1')
        junction_2 = threading.Thread(target=oneTotwo2.computeFlow, args=(Q3_num1, Q3_num2, jt2_flow_in),
                                      name='junction2')

        pipe_1 = threading.Thread(target=pipe1.flow, args=(jt1_flow_in, pipe1_pool1), name='pipe1')
        pipe_2 = threading.Thread(target=pipe2.flow, args=(jt2_flow_in, pipe2_pool2), name='pipe2')

        pool1 = threading.Thread(target=pool_1.decrease_fluid, args=(Q2_num1,), name='deceasefluid')
        pool2 = threading.Thread(target=pool_2.increase_fluid, args=(Q3_num1,), name='increasefluid')

        produceFrequency1.start()
        produceFrequency2.start()

        produceFlow1.start()
        produceRaise1.start()

        produceFlow2.start()
        produceRaise2.start()

        junction_1.start()
        junction_2.start()

        time.sleep(0.2)

        pipe_1.start()
        pipe_2.start()

        pool1.setDaemon(True)
        pool2.setDaemon(True)

        pool1.start()
        pool2.start()

        produceFlow1.join()
        produceRaise1.join()

        pool1.join()
        pool2.join()
